package com.saodisheng.service.impl;

import com.jfinal.plugin.activerecord.Page;
import com.saodisheng.model.Posts;
import com.saodisheng.model.Tags;
import com.saodisheng.service.AccountService;
import com.saodisheng.service.AdminTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description:
 *
 * @author 扫地生_saodisheng
 * @date 2022/1/29
 */
@Service
public class AdminTagServiceImpl implements AdminTagService {
	@Autowired
	private AccountService accountService;

	final Tags tagDao = new Tags().dao();
	final Posts postDao = new Posts().dao();
	int pageSize = 15;
	
	@Override
	public List<Tags> getAllTag() {
		List<Tags> tags = tagDao.find("select * from tags");
		
		return tags;
	}
	
	@Override
	public Page<Posts> getPostPageByTag(String tagName, int pageNumber)
	{
		String select = "select * ";
		String from = "from posts where locate(?,tags)>0 order by createAt desc";
		
		Page<Posts> postPage = postDao.paginate(pageNumber, pageSize, select, from, tagName);
		
		accountService.join("userId", postPage.getList(), "nickName", "avatar");
		return postPage;
	}
}
